﻿Partial Public Class Test
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Create temporary test roles and users
    Dim strRoles() As String = {Guid.NewGuid.ToString, Guid.NewGuid.ToString, Guid.NewGuid.ToString}
    Dim strUsers() As String = {Guid.NewGuid.ToString, Guid.NewGuid.ToString, Guid.NewGuid.ToString}
    Dim intInitialRoleCount As Integer = Roles.GetAllRoles.Count

    Call Roles.CreateRole(strRoles(0))
    Call Roles.CreateRole(strRoles(1))
    Call Roles.CreateRole(strRoles(2))
    lblCreateRole.Text = "OK!"

    ' Get Roles
    If Roles.GetAllRoles().Count >= 3 Then
      lblGetAllRoles.Text = "OK!"
    End If
    If Roles.RoleExists(strRoles(0)) AndAlso Roles.RoleExists(strRoles(0)) AndAlso Roles.RoleExists(strRoles(2)) Then
      lblRoleExists.Text = "OK!"
    End If

    ' Add users
    Call Roles.AddUsersToRoles(strUsers, strRoles)
    lblAddUsersToRoles.Text = "OK!"

    ' Find users
    If Roles.FindUsersInRole(strRoles(0), strUsers(0)).Count = 1 Then
      lblFindUsersInRole.Text = "OK!"
    End If
    If Roles.GetUsersInRole(strRoles(0)).Count = 3 Then
      lblGetUsersInRole.Text = "OK!"
    End If

    ' Find roles for user
    If Roles.GetRolesForUser(strUsers(0)).Count = 3 Then
      lblGetRolesForUser.Text = "OK!"
    End If
    If Roles.IsUserInRole(strUsers(2), strRoles(2)) Then
      lblIsUserInRole.Text = "OK!"
    End If

    ' Fail to delete role
    Try
      Call Roles.DeleteRole(strRoles(0), True)
    Catch ex As Provider.ProviderException
      ' It's ok, was expected, move along... :)
    End Try

    ' Remove users
    Call Roles.RemoveUsersFromRoles(strUsers, strRoles)
    If Roles.GetUsersInRole(strRoles(0)).Count = 0 Then
      lblRemoveUsersFromRoles.Text = "OK!"
    End If

    ' Delete roles
    Call Roles.DeleteRole(strRoles(0))
    Call Roles.DeleteRole(strRoles(1))
    Call Roles.DeleteRole(strRoles(2))

    If Roles.GetAllRoles.Count = intInitialRoleCount Then
      lblDeleteRole.Text = "OK!"
    End If
  End Sub
End Class